package com.cq.controller;

import com.cq.pojo.Building;
import com.cq.pojo.Dorm;
import com.cq.pojo.LoginUser;
import com.cq.pojo.Visit;
import com.cq.service.BuildService;
import com.cq.service.LoginService;
import com.cq.service.VisitService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author lap
 * @version 1.0
 * @date 2021/8/12 11:23
 */
@Controller
@RequestMapping("/student")
public class LoginController {

    @Autowired
    private LoginService loginService;

    @Autowired
    private BuildService buildService;

    @Autowired
    private VisitService visitService;


    @RequestMapping("/to_login")
    public String to_login(){
        return "login";
    }

    @RequestMapping("/login")
    public String login(HttpServletRequest request, RedirectAttributes attributes){
        String username = request.getParameter("username");
        char c = username.charAt(0);
        String password = request.getParameter("password");
        String role = request.getParameter("role");
        LoginUser loginUser=null;
        if(c+'0'>=0 && c+'0'<=9){
            loginUser= loginService.QueryLoginByStudent(Integer.parseInt(username), password);
        }else{
            loginUser= loginService.QueryLoginByEmp(username, password);
        }
        HttpSession session = request.getSession();
        session.setAttribute("loginUser",loginUser);
        if(loginUser == null){
            attributes.addFlashAttribute("message","1");
            return "redirect:/student/to_login";
        }
        return "redirect:/student/loginSuccess";
    }

    @RequestMapping("/loginSuccess")
    public String loginSuccess(){

        return "success";
    }

    @RequestMapping("/logout")
    public String logout(HttpServletRequest request){
        Subject subject = SecurityUtils.getSubject();
        subject.logout();
        request.getSession().removeAttribute("loginUser");
        return "login";
    }

    @RequestMapping("/shown")
    public String show(){
        return "show";
    }

/*    @RequestMapping("/to_visit")
    public String visit(){
        return "visit";
    }*/

    @RequestMapping("/to_visit")
    public String queryBuildName(Model model){
        List<Building> buildings = buildService.selectAllBuildName();
        model.addAttribute("builds",buildings);
        return "visit";
    }

    @RequestMapping("/admin/to_visitMessage")
    public String to_visitMessage(Model model){
        List<Building> buildings = buildService.selectAllBuildName();
        model.addAttribute("builds",buildings);
        return "visitMessage";
    }

    @RequestMapping("/roomId")
    @ResponseBody
    public List<Dorm> roomId(String build_name){
        System.out.println("========================>"+build_name);
        List<Building> build_ids = buildService.build_id(build_name);
        Integer build_id=0;
        for (Building one:build_ids){
            System.out.println(one);
            build_id=one.getBuild_Id();
        }
        System.out.println("------------------------>"+build_id);
        List<Dorm> dorms = buildService.selectAllDorm(build_id);
        return dorms;
    }

    @RequestMapping("/visit")
    public String visit(HttpServletRequest request,RedirectAttributes attributes,Model model){
        String v_name =  request.getParameter("shipping01")+request.getParameter("shipping02");
        String v_sex = request.getParameter("sex");
        String v_tel = request.getParameter("tel");
        String build_name = request.getParameter("build_name");
        String room_id = request.getParameter("room_id");
        String v_ToRoom = build_name+room_id;
        String v_thing = request.getParameter("things");
        String v_time = request.getParameter("times");

        Visit visit = new Visit(v_name,v_sex,v_tel,v_ToRoom,v_thing,v_time);
        Integer integer = visitService.insertVisit(visit);
        if(integer<=0){
            attributes.addFlashAttribute("message","0");
            return "redirect:/student/to_visit";
        }
        model.addAttribute("name",v_name);
        return "visitSuccess";
    }

    @RequestMapping("/admin/visitMessage")
    public String visitMessage(HttpServletRequest request,RedirectAttributes attributes,Model model){
        String v_name =  request.getParameter("shipping01")+request.getParameter("shipping02");
        String v_sex = request.getParameter("sex");
        String v_tel = request.getParameter("tel");
        String build_name = request.getParameter("build_name");
        String room_id = request.getParameter("room_id");
        String v_ToRoom = build_name+room_id;
        String v_thing = request.getParameter("things");
        String v_time = request.getParameter("times");
        Visit visit = new Visit(v_name,v_sex,v_tel,v_ToRoom,v_thing,v_time);

        List<Visit> visits = visitService.selectVisit(visit);
        model.addAttribute("visits",visits);
        return "visitTable";
    }

}
